package com.xoa.dev.workFlow.service;

import com.xoa.dao.WFE.WFEFlowTypeModelMapper;
import com.xoa.dao.common.SysParaMapper;
import com.xoa.model.common.SysPara;
import com.xoa.model.users.UserGroup;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowTypeModel;
import com.xoa.service.WFE.WFERunner.WFEFlowRunInfo;
import com.xoa.service.users.UserGroupService;
import com.xoa.util.AjaxJson;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Service
public class WorkFlowSelectUserService {


    @Autowired
    UserGroupService userGroupService;

    @Autowired
    WFEFlowRunInfo wfeFlowRunInfo;

    @Autowired
    WFEFlowTypeModelMapper wfeFlowTypeModelMapper;

    public AjaxJson getWFUserGroup(HttpServletRequest request, Integer flowId, Integer flowPrcs, Users user, Integer type,
                                   Integer deptId, Integer userPriv, String deptNo, String search, PageParams pageParams) {
        AjaxJson ajaxJson = new AjaxJson();
        try {
            ToJson<UserGroup> toJson = new ToJson<UserGroup>();
            //调用获取分组接口1 个人  2公共
            if (1 == type) {
                toJson = userGroupService.getAllUserGroup1(request);
            } else {
                toJson = userGroupService.getAllUserGroup();
            }

            List<UserGroup> userGroupList = toJson.getObj();
            if (userGroupList == null || userGroupList.size() == 0) {
                return null;
            }
            //返回的分组
            List<UserGroup> resultUserGroup = new ArrayList<UserGroup>();
            //返回的分组中的用户
            List<Users> resultUsers;
            //返回的结果
            Map<Object, Object> result = new HashMap<Object, Object>();
            //工作流此步骤可选人员
            List<Users> temp = null;

            FlowTypeModel flowTypeModel = wfeFlowTypeModelMapper.queryOneObject(flowId);

            boolean flag = false;
            if (flowTypeModel!=null && "1".equals(flowTypeModel.getGroupUserYn())) {
                flag = true;
            } else {
                //调用
                ajaxJson = wfeFlowRunInfo.getWorkFlowUser(flowId, flowPrcs, user, deptId, userPriv, deptNo, search, pageParams);
                //工作流此步骤可选人员
                temp = (List<Users>) ajaxJson.getObj();
                ajaxJson = new AjaxJson();
            }

            for (UserGroup userGroup : userGroupList) {
                String userStr = userGroup.getUserStr();
                if (!StringUtils.checkNull(userStr)) {
                    String[] userIds = userStr.split(",");
                    if (flag) {
                        resultUsers = (List<Users>) userGroupService.getUsersByGroupId(userGroup.getGroupId().toString()).getObject();

                    } else {
                        resultUsers = new ArrayList<Users>();
                        for (Users u : temp) {
                            if (Arrays.asList(userIds).contains(u.getUserId())) {
                                resultUsers.add(u);
                            }
                        }
                    }
                    if (resultUsers.size() > 0) {
                        userGroup.setUsersList(resultUsers);
                        resultUserGroup.add(userGroup);
                    }

                }
            }
            result.put("userGroupList", resultUserGroup);
            ajaxJson.setObj(result);
            ajaxJson.setMsg("OK");
            ajaxJson.setFlag(true);
        } catch (Exception e) {
            ajaxJson.setMsg(e.getMessage());
            ajaxJson.setFlag(false);
            throw e;
        }
        return ajaxJson;

    }
}
